//
//  PAMailDetailHeadCell.m
//  iOS_Router_Runtime
//
//  Created by 王水鑫 on 2018/11/1.
//  Copyright © 2018年 王水鑫. All rights reserved.
//

#import "PAMailDetailHeadCell.h"
#import "UIColor+ColorChange.h"

static CGFloat const switchDetailInfoButtonTitleFont = 14;
static NSString * const switchDetailInfoButtonTitleTypeface = @"PingFangSC-Regular";
static CGFloat const senderNameLabTitleFont = 18;
static NSString * const senderNameLabTitleTypeface = @"PingFangSC-Medium";
static CGFloat const receiverNameLabTitleFont = 14;
static NSString * const receiverNameLabTitleTypeface = @"PingFangSC-Regular";
static CGFloat const receiverTipLabelTitleFont = 18;
static NSString * const receiverTipLabelTitleTypeface = @"PingFangSC-Medium";
static CGFloat const senderNameButtonTitleFont = 18;
static NSString * const senderNameButtonTitleTypeface = @"PingFangSC-Medium";

@interface PAMailDetailHeadCell () {
    int indexTag;
}
@property (strong, nonatomic) UIView *fixedContainer;
@property (strong, nonatomic) UIView *flexibleContainer;
@property (assign, nonatomic) CGFloat cellHeight;
@end

@implementation PAMailDetailHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)init {
    if (self = [super init]) {
        [self appendSubViews];
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone; // 去掉点击阴影
    return self;
}
- (void)appendSubViews {
    // 切换详情
    UIButton *switchDetailInfoButton = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 30 - 20 , 41, 30, 20)];
    [switchDetailInfoButton setTitle:@"详情" forState:UIControlStateNormal];
    [switchDetailInfoButton setTitle:@"隐藏" forState:UIControlStateSelected];
    [switchDetailInfoButton setTitleColor:[UIColor colorWithHexString:@"#0091E7"] forState:UIControlStateNormal];
    [switchDetailInfoButton.titleLabel setFont:[UIFont fontWithName:switchDetailInfoButtonTitleTypeface size:switchDetailInfoButtonTitleFont]];
    [switchDetailInfoButton addTarget:self action:@selector(detailInfoSwitchFunctionMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:switchDetailInfoButton];
    
    
    //固定容器初始化
    self.fixedContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 60, 70)];
    self.fixedContainer.hidden = NO;
    [self addSubview:self.fixedContainer];
    //可变容器初始化
    self.flexibleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 60, 110)];
    self.flexibleContainer.hidden = YES;
    [self addSubview:self.flexibleContainer];
    // 默认添加固定行高的容器的子视图
    [self appendFixedContainer];
    // 控制详情页面创建一次的参数
    indexTag = 0;
}

- (void)detailInfoSwitchFunctionMethod:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSLog(@"cell中动态的不封");
        if (indexTag == 0) {
            // 给可变容器加子视图
            [self appendFlexibleContainer];
        }
        self.fixedContainer.hidden = YES;
        self.flexibleContainer.hidden = NO;
        self.switchDetailInfo(sender.selected, self.cellHeight);
    } else {
        NSLog(@"cell中固定行高");
        self.fixedContainer.hidden = NO;
        self.flexibleContainer.hidden = YES;
        self.switchDetailInfo(sender.selected, 70);
    }
    indexTag ++;
}
- (void)appendFixedContainer {
    // 发件人
    self.senderNameLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 8, 300, 26)];
    [self.senderNameLab setFont:[UIFont fontWithName:senderNameLabTitleTypeface size:senderNameLabTitleFont]];
    self.senderNameLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [self.fixedContainer addSubview:self.senderNameLab];

    // 收件人
    self.receiverNameLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 39, [UIScreen mainScreen].bounds.size.width - 90, 20)];
    [self.receiverNameLab setFont:[UIFont fontWithName:receiverNameLabTitleTypeface size:receiverNameLabTitleFont]];
    self.receiverNameLab.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.fixedContainer addSubview:self.receiverNameLab];
}
- (void)appendFlexibleContainer {
//    detailHeadContentHeightConstraint
    UILabel *receiverTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 8, 80, 20)];
    receiverTipLabel.text = @"发件人:";
    [receiverTipLabel setFont:[UIFont fontWithName:receiverTipLabelTitleTypeface size:receiverTipLabelTitleFont]];
    receiverTipLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    [self.flexibleContainer addSubview:receiverTipLabel];
    
    // 发件人
    CGSize senderNameLabSize = [self.senderNameLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.senderNameLab.font,NSFontAttributeName, nil]]; // 获取发件人字体所占宽度
    CGFloat senderNameLabWidth = senderNameLabSize.width <= [UIScreen mainScreen].bounds.size.width - 160 ? senderNameLabSize.width : [UIScreen mainScreen].bounds.size.width - 160;
    UIButton *senderNameButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 8, senderNameLabWidth, 20)];
    [senderNameButton setTitle:self.senderNameLab.text forState:UIControlStateNormal];
    [senderNameButton.titleLabel setFont:[UIFont fontWithName:senderNameButtonTitleTypeface size:senderNameButtonTitleFont]];
    [senderNameButton setTitleColor:[UIColor colorWithHexString:@"#3E97CB"] forState:UIControlStateNormal];
    [senderNameButton addTarget:self action:@selector(alertSheet:) forControlEvents:UIControlEventTouchUpInside];
    [self.flexibleContainer addSubview:senderNameButton];
    
    // 36
    // 收件人列表
    UILabel *detailReceiverLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 36, 60, 20)];
    detailReceiverLab.font = [UIFont fontWithName:receiverNameLabTitleTypeface size:receiverNameLabTitleFont];
    detailReceiverLab.textColor = [UIColor colorWithHexString:@"#999999"];
    detailReceiverLab.text = @"收件人:";
    [self.flexibleContainer addSubview:detailReceiverLab];
    // 收件人buttonList创建
    CGRect frontFrame = detailReceiverLab.frame;
    frontFrame = [self createContactList:self.receiversArray frame:frontFrame];
    
    // 抄送列表
    UILabel *detailCopyLab = [[UILabel alloc] initWithFrame:CGRectMake(20, frontFrame.origin.y + 24, 60, 20)];
    detailCopyLab.font = [UIFont fontWithName:receiverNameLabTitleTypeface size:receiverNameLabTitleFont];
    detailCopyLab.textColor = [UIColor colorWithHexString:@"#999999"];
    detailCopyLab.text = @"抄   送:";
    [self.flexibleContainer addSubview:detailCopyLab];
    // 抄送buttonList创建
    frontFrame = detailCopyLab.frame;
    frontFrame = [self createContactList:self.copiersArray frame:frontFrame];
    
    self.cellHeight = frontFrame.origin.y + 28;
}
- (CGRect)createContactList:(NSArray *)array frame:(CGRect)frontFrame{
    for (int i = 0; i < array.count; i ++) { // font: 14
        UIButton *receiverBtn = [[UIButton alloc] init];
        receiverBtn.titleLabel.font = [UIFont fontWithName:receiverNameLabTitleTypeface size:receiverNameLabTitleFont];
        [receiverBtn setTitle:[NSString stringWithFormat:@"%@>", array[i]] forState:UIControlStateNormal];
        [receiverBtn setTitleColor:[UIColor colorWithHexString:@"#3E97CB"] forState:UIControlStateNormal];
        [receiverBtn addTarget:self action:@selector(alertSheet:) forControlEvents:UIControlEventTouchUpInside];
        CGSize size = [receiverBtn.currentTitle sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:receiverBtn.titleLabel.font,NSFontAttributeName, nil]];
        if (frontFrame.origin.x + frontFrame.size.width + size.width + 6 > [UIScreen mainScreen].bounds.size.width - 60) {
            frontFrame = CGRectMake(- frontFrame.size.width, frontFrame.origin.y + 24, frontFrame.size.width, frontFrame.size.height);
        }
        receiverBtn.frame = CGRectMake(frontFrame.origin.x + frontFrame.size.width, frontFrame.origin.y, size.width + 6, 20);
        frontFrame = receiverBtn.frame;
        [self.flexibleContainer addSubview:receiverBtn];
    }
    return frontFrame;
}
- (void)alertSheet:(UIButton *)sender {
    NSLog(@"跳联系人详情信息！");
}
@end
